home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
crefc.zip
/
CREFC.C
< prev
next >
Wrap
Text File
|
1993-01-04
|
5KB
|
177 lines
#define YES 1
#define NO 0
#include "stdio.h"
#include "ctype.h"
struct words {
char symbol[15];
struct words *left; /*left daughter*/
struct words *right; /*right daughter*/
struct lines *lineptr; /*pointer to linked list of line numbers*/
};
struct lines {
int lineno; /*line numbers*/
struct lines *nxtline; /*pointer to next structure*/
};
main(number,name)
int number;
char *name[];
{
FILE *infile, *outfile;
char word[20],*ptr;
int lineno,incomm,inword,instr,c,pchar,mon,year,day;
struct words *rootword,*saveword();
rootword=NULL; /*first pointer to the root of the tree*/
if(number<2)
{
printf("No file specified");
exit();
}
if((infile=fopen(name[1],"r"))==NULL)
{
printf("Can't open %s",name[1]);
exit();
}
if((outfile=fopen(name[2],"w"))==NULL)
{
printf("Can't open %s",name[2]);
exit();
}
ptr=word;
incomm=NO;
instr=NO;
lineno=1;
inword=NO;
while((c=fgetc(infile))!=EOF)
{
if(c=='\042'&&instr) /*is it a quote*/
instr=NO;
else if(c=='\042'&&!instr)
instr=YES;
if(c=='*' && pchar=='/') /*is it a comment*/
{
incomm=YES;
pchar=c;
continue;
}
else if(c=='/' && pchar=='*')
{
incomm=NO;
pchar=c;
continue;
}
/*If this is true it is the end of a word*/
if(!instr&&!incomm&&(ispunct(c)||isspace(c)||c=='\n'))
{
*ptr='\0'; /*zap null to end of string*/
if(strcmp(word,"")!=0 && isalnum(pchar))
{
if(!iskey(word)) /*is is a keyword*/
/*put the word in the tree*/
rootword=saveword(word,lineno,rootword);
ptr=word; /*reset pointer*/
}
}
/*if this is true add the character to the string*/
else if(!instr&&!incomm&&isalnum(c))
*ptr++=c;
if(c=='\n') /*newline, so increment line counter*/
lineno++;
pchar=c;
}
sysdate(&mon,&day,&year); /*get sysdate for printer*/
fprintf(outfile,"SYMBOL CROSS REFERENCE\n");
fprintf(outfile,"File Name: %s\n",name[1]);
fprintf(outfile,"Date printed: %d/%d/%d\n",mon,day,year);
fprintf(outfile,"\n\nSYMBOL LINES WHERE SYMBOL IS FOUND\n");
display(rootword,outfile); /*display the tree*/
}
struct words *saveword(w,line,p)/*put the words in a tree*/
struct words *p;
char *w;
int line;
{
struct words *wrdalloc();
struct lines *linealloc(),*addline();
int cond;
if(p==NULL) /*end of the branch*/
{
p=wrdalloc(); /*allocate some memory for the structure*/
strcpy(p->symbol,w);
p->left=p->right=NULL; /*set new daughters*/
p->lineptr=linealloc(); /*get structure pointer for lineno*/
p->lineptr->lineno=line;/*set lineno*/
p->lineptr->nxtline=NULL;/*set nextline number pointer*/
}
else if((cond=strcmp(w,p->symbol))==0)/*does the word already exist*/
p->lineptr=addline(p->lineptr,line);/*add the line number*/
else if(cond<0) /*look in the left branch*/
p->left=saveword(w,line,p->left);
else
p->right=saveword(w,line,p->right);/*look in the right branch*/
return(p);
}
struct words *wrdalloc() /*memory allocation for the words structure*/
{
char *getmem();
return((struct words *)getmem(sizeof(struct words)));
}
struct lines *addline(ptr,line) /*add a new line to the list of lines*/
struct lines *ptr;
int line;
{
struct lines *linealloc();
if(ptr==NULL) /*is it the end of the list*/
{
ptr=linealloc(); /*allocate some memory*/
ptr->nxtline=NULL; /*set the pointers and line number*/
ptr->lineno=line;
}
else /*look for the end of the list*/
ptr->nxtline=addline(ptr->nxtline,line);
return(ptr);
}
struct lines *linealloc() /*memory allocator for the lines structure*/
{
char *getmem();
return((struct lines *)getmem(sizeof(struct lines)));
}
display(p,outf) /*displays the tree*/
FILE *outf;
struct words *p;
{
struct lines *lptr;
int i;
if(p!=NULL)
{
display(p->left,outf); /*display the left tree*/
fprintf(outf,"%-8.8s ",p->symbol);
for(lptr=p->lineptr,i=1;lptr!=NULL;lptr=lptr->nxtline,i++)
{
fprintf(outf,"%4d ",lptr->lineno); /*display the line numbers*/
if(i%14==0&&lptr->nxtline!=NULL)
fprintf(outf,"%-8s "," ");
}
fputc('\n',outf);
display(p->right,outf); /*display the right tree*/
}
}
/*Check if the word is a C keyword*/
iskey(word)
char *word;
{
static char *keyword[]={
"int","extern","else","char","register","for","float","typedef","do","double",
"static","while","struct","goto","switch","union","return","case","long",
"sizeof","default","short","break","entry","unsigned","continue","auto",
"if","include","define"};
int i;
for(i=0;i<sizeof keyword/sizeof(char *);i++)
if(strcmp(keyword[i],word)==0)
return(1);
return(0);
}